<template>
  <!-- 头部查询、新增栏 -->
  <el-form :inline="true" class="demo-form-inline">
    <el-form-item>
      <el-input v-model.trim="user_name" placeholder="输入跟单人查询" />
    </el-form-item>
    <el-form-item>
      <el-input v-model.trim="supplier_name" placeholder="输入供应商查询" />
    </el-form-item>
    <el-form-item>
      <el-select
        v-model="state"
        placeholder=" "
        collapse-tags-tooltip
        style="width: 200px"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="queryOrder">查询</el-button>
      <el-button @click="handelAddOrder">新建跟进订单</el-button>
    </el-form-item>
  </el-form>

  <!-- 分割线 -->
  <el-divider />

  <!-- 表格数据 -->
  <el-table :data="orderList" border>
    <el-table-column fixed prop="apply_id" label="采购单号" width="240" />
    <el-table-column prop="state" label="状态" width="150" />
    <el-table-column prop="createdAt" label="建单时间 " width="200" />
    <el-table-column prop="apply_type" label="申请类型" width="200" />
    <el-table-column prop="user_name" label="跟单人" width="200" />
    <el-table-column prop="supplier_name" label="供应商名称" width="250" />
    <el-table-column prop="apply_reason" label="申请原因" width="350" />
    <el-table-column prop="remark" label="备注" width="350" />
    <el-table-column fixed="right" prop="operation" label="操作" width="200">
      <a href="#">详情</a>
    </el-table-column>
    <template #empty>
      <el-empty description="暂无数据" />
    </template>
  </el-table>
  <div class="pagination">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="sizes"
      @current-change="handelPages"
    />
  </div>
</template>

<script>
import { purOrderQuery } from "@/apis/Purchase/purOrder";

export default {
  data() {
    return {
      supplier_name: "",
      user_name: "",
      orderList: [],
      total: 0,
      state: "",
      sizes: 10,
      pages: 1,
      options: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "New",
          label: "新建",
        },
        {
          value: "Add",
          label: "下发单据到供应商",
        },
        {
          value: "Follow",
          label: "跟进中",
        },
        {
          value: "4",
          label: "在途",
        },
        {
          value: "Archive",
          label: "归档验货",
        },
        {
          value: "Cancel",
          label: "取消",
        },
      ],
    };
  },

  created() {
    this.findOrderList();
  },

  methods: {
    //查询供应商数据
    async findOrderList(
      supplier_name,
      user_name,
      state,
      size = this.sizes,
      page = this.pages
    ) {
      try {
        const result = await purOrderQuery({
          supplier_name,
          user_name,
          state,
          size,
          page,
        });
        this.orderList = result.data.rows;
        this.total = result.data.count;
      } catch (error) {
        console.log("error:", error);
      }
    },

    //查询供应商信息
    queryOrder() {
      this.orderList.forEach((item) => {
        if (item.user_name.includes(this.user_name)) {
          this.findOrderList(this.supplier_name, this.user_name, this.state);
        } else if (item.supplier_name.includes(this.supplier_name)) {
          this.findOrderList(this.supplier_name, this.user_name, this.state);
        } else if (item.state === this.state) {
          this.findOrderList(this.supplier_name, this.user_name, this.state);
        } else {
          this.findOrderList();
        }
      });
    },

    handelAddOrder() {
      this.$router.push("/purchase/order/edit");
    },

    // 页码改变，翻页查询数据
    handelPages(e) {
      this.pages = e;
      this.findOrderList();
    },
  },
};
</script>

<style lang="less" scoped>
.el-form {
  float: right;
  line-height: 50px;
  .el-form-item {
    margin: 0 10px;
  }
}
.el-divider {
  margin-top: 50px;
}
a {
  display: inline-block;
  text-decoration: none;
  width: 30px;
  font-size: 12px;
}
.pagination {
  width: 100%;
  height: 50px;
  position: fixed;
  left: 0;
  bottom: 50px;
  // .el-pagination {
  //    margin: 13px calc(100% / 2);
  // }
}
</style>
